/**
 * Created by rongyao on 2017/10/31.
 */


export default {
	data(){
		return {
			modal:false,
			treeData:[],
			currentNode:[],
			formValidate:{
				title:"",
				type:""
			},
			ruleValidate: {
				title: [
					{ required: true, message: '节点名不能为空', trigger: 'blur' }
				]
			}
		}
	},
	methods:{
		onSelectChange(node){
			if(node&&node.length>0){
				this.currentNode=node
				this.cateId ={_id:node[0]._id};
				var ids = [];
				this.$http.post(`http://localhost:3000/cate/list/${node[0]._id}`).then(res=>{
					ids = res.data;
					this.filter.ids=ids;
					if(this.getData){
						this.getData();
					}
				})
				
			}else{
				this.filter.ids = []
				this.currentNode=[];
				this.cateId={};
				if(this.getData){
					this.getData();
				}
			}
			
		},
		addChildNode(){
			this.formValidate = this.$options.data().formValidate;
			if(this.currentNode&&this.currentNode.length>0){
				this.formValidate.parentId = this.currentNode[0]._id;
				this.modal=true
			}else{
				this.$Message.success('请选择要添加到的节点!');
			}
		},
		addRootNode(){
			this.formValidate = this.$options.data().formValidate;
			this.modal=true
		},
		removeRootNode(){
			this.formValidate = this.$options.data().formValidate;
			if(this.currentNode&&this.currentNode.length>0){
				this.formValidate._id = this.currentNode[0]._id;
				this.$Modal.confirm({
					title: '数据操作',
					content: '<p>确认删除数据吗</p>',
					onOk: () => {
						this.$http.delete(`http://localhost:3000/cate/data/${this.formValidate._id}`).then(res=>{
							this.$Message.success('删除成功!');
							this.getTreeData()
						})
					},
					onCancel: () => {
//						this.$Message.info('点击了取消');
					}
				});
			}else{
				this.$Message.success('请选择要删除的节点!');
			}
		},
		updataNode(){
			if(this.currentNode&&this.currentNode.length>0){
				this.formValidate = this.currentNode[0];
				this.modal=true
			}else{
				this.$Message.success('请选择要修改的节点!');
			}
		},
		handleSubmit(name){
			this.formValidate.type=this.type;
			this.$refs[name].validate((valid) => {
				if (valid) {
					if(this.formValidate._id&&this.formValidate._id.length>0){
						this.$http.post(`http://localhost:3000/cate/data/${this.formValidate._id}`,this.formValidate).then(res=>{
							this.$Message.success('修改成功!');
							this.modal=false;
							this.getTreeData()
						})
					}else{
						this.$http.post('http://localhost:3000/cate/data',this.formValidate).then(res=>{
							this.$Message.success('添加成功!');
							this.modal=false;
							this.getTreeData()
						})
					}
					
				} else {
					this.$Message.error('表单验证失败!');
				}
			})
		},
		getTreeData(){
			this.$http.post(`http://localhost:3000/cate/list`,{type:this.type}).then(res=>{
					this.treeData=res.data;
					this.currentNode = [];
			})
		}
	},
	created(){
		this.getTreeData()
	}

}
